#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 1e6 + 10;

int n, pot[N], top, fa[N], to[N], stk[N];
ll mxd[N], s[N], diam, ans;
vector < tuple < int, int, int > > G[N];
bool vis[N], ban[N];
bool fl;

void dfs(int x, int p) {
	vis[x] = true; 
	for(auto v : G[x]) {
		int y, w, id; tie(y, w, id) = v;
		if(id == p) continue;
		if(!vis[y]) to[y] = w, fa[y] = x, dfs(y, id);
		else {
			int u = x, lst = w;
			while(u != y) {
				stk[++top] = u; s[top] = lst; lst = to[u];
				u = fa[u];
			}
			stk[++top] = y; s[top] = lst;
			rep(i, 1, top) stk[top + i] = stk[i], s[top + i] = s[i], ban[stk[i]] = true;
			rep(i, 1, top << 1) s[i] += s[i - 1];
			fl = true;
		} if(fl) return;
	}
}

void tdfs(int x, int p) {
	mxd[x] = 0;
	for(auto y : G[x]) 
		if(get<2>(y) != p && !ban[get<0>(y)])
			tdfs(get<0>(y), get<2>(y)), chkmax(diam, mxd[x] + mxd[get<0>(y)] + get<1>(y)), chkmax(mxd[x], mxd[get<0>(y)] + get<1>(y));
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin); freopen("CF835F.out", "w", stdout);
#endif
	n = in; rep(i, 1, n) { int u = in, v = in, w = in; G[u].eb(v, w, i), G[v].eb(u, w, i); }
	dfs(1, 0); rep(i, 1, top) tdfs(stk[i], 0);
	
	priority_queue < pair < ll, int > > q1, q2;
	ans = 1e18;
	rep(i, 1, top << 1) {
		q2.ep(s[i] + mxd[stk[i]], i);
		while(q1.size() && q1.top().se <= i - top) q1.pop();
		while(q2.size() && q2.top().se <= i - top) q2.pop();
		if(i >= top) {
			if(q1.top().se == q2.top().se) {
				auto v1 = q1.top(), v2 = q2.top(); q1.pop(); q2.pop();
				while(q1.top().se <= i - top) q1.pop();
				while(q2.top().se <= i - top) q2.pop();
				chkmin(ans, max(v1.fi + q2.top().fi, v2.fi + q1.top().fi));
				q1.ep(v1); q2.ep(v2);
			} else chkmin(ans, q1.top().fi + q2.top().fi);
		} q1.ep(-s[i] + mxd[stk[i]], i);
	}
	printf("%lld\n", max(ans, diam));
	return 0;
}
